package mianyang.zhangsan.com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.pojo.Course;
import mianyang.zhangsan.com.pojo.Score;
import mianyang.zhangsan.com.pojo.SelectedCourse;
import mianyang.zhangsan.com.service.CourseService;
import mianyang.zhangsan.com.service.ScoreService;
import mianyang.zhangsan.com.service.SelectedCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private SelectedCourseService selectedService;

    /**
     * 课程列表查询
     * @param page
     * @param rows
     * @param teacherId
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(value = "page", defaultValue = "1")int page,
                       @RequestParam(value = "rows", defaultValue = "10")int rows,
                       @RequestParam(value = "teacherId", defaultValue = "0")int teacherId){
        Page<Course> coursePage = courseService.queryByPage(page, rows, teacherId);
        Map<String, Object> map = new HashMap<>();
        map.put("total", coursePage.getPages());
        map.put("rows", coursePage.getRecords());
        return map;
    }

    /**
     * 课程复选框查询
     * @param studentId
     * @return
     */
    @RequestMapping("/comboboxList")
    @ResponseBody
    public Object comboboxList(Integer studentId){
        List<Course> courseList = null;
        if (studentId != null){
            List<Score> scoreList = scoreService.queryCourseByStudentId(studentId);
            courseList = new ArrayList<>();
            for(Score score:scoreList){
                Course course = courseService.queryInfoById(score.getCourseId());
                courseList.add(course);
            }
        }else{
            courseList = courseService.queryInfoAll();
        }
        return courseList;
    }

    /**
     * 添加课程信息
     * @param course
     * @return
     */
    @RequestMapping(value = "/add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addCourse(Course course){
        if(course != null){
            courseService.addCourse(course);
            return "{\"success\": true, \"message\":\"添加成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 删除课程信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteCourse(int id){
        if(id != 0){
            courseService.deleteCourse(id);
            return "{\"success\": true, \"message\":\"删除成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 选课列表查询
     * @return
     */
    @RequestMapping("/selectList")
    @ResponseBody
    public Object selectedList(@RequestParam(value = "page", defaultValue = "1")int page,
                               @RequestParam(value = "rows", defaultValue = "10")int rows,
                               @RequestParam(value = "studentId", defaultValue = "0")int studentId,
                               @RequestParam(value = "courseId", defaultValue = "0")int courseId){
       Page<SelectedCourse> selectedCoursePage = selectedService.queryByPage(page, rows, studentId, courseId);
       Map<String, Object> map = new HashMap<>();
       map.put("total", selectedCoursePage.getPages());
       map.put("rows", selectedCoursePage.getRecords());
       return map;
    }

    /**
     * 添加选课信息
     * @return
     */
    @RequestMapping("/selectAdd")
    @ResponseBody
    public Object selectAdd(SelectedCourse selectedCourse){
        if (selectedCourse != null){
            selectedService.addSelected(selectedCourse);
            return "{\"success\": true, \"message\":\"添加成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 删除选课信息
     * @return
     */
    @RequestMapping("/selectDelete")
    @ResponseBody
    public Object selectDelete(int id){
        if (id != 0){
            selectedService.deleteSelected(id);
            return "{\"success\": true, \"message\":\"删除成功\"}";
        }
        return "{\"success\": false}";
    }
}
